package net.homelinux.chaoswg.io.renga.krypto.animations.Pht;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import net.homelinux.chaoswg.io.renga.core.registries.GlobalPropertyRegistry;
import net.homelinux.chaoswg.io.renga.krypto.datatypes.numerical.UnsignedNBit;

public class PhtStateChanger implements Runnable {
    final PhtState initialState;
    JLabel firstOperandsDisplay;
    JLabel secondOperandsDisplay;
    JLabel firstResultDisplay;
    JLabel secondResultDisplay;
    JFrame jw;


    public PhtStateChanger (PhtState initialState) {
        this.initialState = initialState;

        jw = new JFrame ("PHT");
        jw.getContentPane().add(getPanel());
        jw.setLocationRelativeTo(null);

        jw.setSize(500, 100);
    }

    public JPanel getPanel() {
        JPanel toReturn = new JPanel (new GridLayout (2,1));
        
        JPanel in = new JPanel (new GridLayout (1,2));
        JPanel out = new JPanel (new GridLayout (1,2));

        in.add(firstOperandsDisplay = new JLabel (initialState.getOperand0().toString()));
        in.add(secondOperandsDisplay = new JLabel (initialState.getOperand1().toString()));
        out.add(firstResultDisplay = new JLabel (initialState.getResult0().toString()));
        out.add(secondResultDisplay = new JLabel (initialState.getResult1().toString()));

        firstOperandsDisplay.setHorizontalAlignment(JLabel.CENTER);
        secondOperandsDisplay.setHorizontalAlignment(JLabel.CENTER);
        firstResultDisplay.setHorizontalAlignment(JLabel.CENTER);
        secondResultDisplay.setHorizontalAlignment(JLabel.CENTER);

        toReturn.add (in);
        toReturn.add(out);
        
        toReturn.setBorder(new BevelBorder (BevelBorder.RAISED));
        return toReturn;
    }

    private void setNew () {
        firstOperandsDisplay.setText (initialState.getOperand0().toString());
        firstOperandsDisplay.paintImmediately(firstOperandsDisplay.getVisibleRect());
        secondOperandsDisplay.setText (initialState.getOperand1().toString());
        secondOperandsDisplay.paintImmediately(secondOperandsDisplay.getVisibleRect());
        firstResultDisplay.setText (initialState.getResult0().toString());
        firstResultDisplay.paintImmediately(firstOperandsDisplay.getVisibleRect());
        secondResultDisplay.setText (initialState.getResult1().toString());
        secondResultDisplay.paintImmediately(firstOperandsDisplay.getVisibleRect());
    }

    public void run() {
        int delay = ((Integer)GlobalPropertyRegistry.getGlobalPropertyRegistry().getProperty("animationSpeed")) * 100;

        jw.setVisible(true);
        
        UnsignedNBit operand0 = initialState.getOperand0();
        UnsignedNBit operand1 = initialState.getOperand1();

        setNew ();

        try {
            Thread.sleep(delay);
        } catch (InterruptedException ie) {System.err.println ("ERR");}


        initialState.setResult0(operand0.add(operand1));
        initialState.setStep(1);

        setNew ();

        try {
            Thread.sleep(delay);
        } catch (InterruptedException ie) {System.err.println ("ERR");}


        initialState.setResult1(operand0.add(operand1).add(operand1));
        initialState.setStep(2);
        setNew ();

        try {
            Thread.sleep(delay);
        } catch (InterruptedException ie) {}
    }

    public void close () {
        jw.dispose ();
    }
}
